package com.crm.customers.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.customers.dao.CommissionCustomerDao;
import com.crm.customers.dao.ContractDao;
import com.crm.customers.dao.ContractMoneylogDao;
import com.crm.customers.dao.CustomerDao;
import com.crm.customers.model.CommissionCustomer;
import com.crm.customers.model.Contract;
import com.crm.customers.model.CustomerInfo;
import com.crm.customers.service.ContractFilesService;
import com.crm.customers.service.ContractMoneylogService;
import com.crm.customers.service.ContractService;
import com.crm.util.Constants;

@Service("contractService")
public class ContractServiceImpl implements ContractService {
	
	@Autowired
	private ContractDao contractDao;
	@Autowired
	private ContractMoneylogService contractMoneylogService;
	@Autowired
	private ContractMoneylogDao contractMoneylogDao;
	@Autowired
	private CustomerDao customerDao;
	@Autowired
	private CommissionCustomerDao commissionCustomerDao;
	@Autowired
	private ContractFilesService contractFilesService;
	@Override
	public List<Contract> getAllMyContractList(Contract c , int pageNo , int pageSize) {
		
		//获取所有我的合同，然后还要获取合同走到哪一步了
		
		c.setStartsize((pageNo-1)*pageSize);
		c.setEndsize(pageSize);
		//先获取所有我的合同列表
		List<Contract> contractlist = contractDao.getAllMyContractListByParams(c);
		//以后要是需要其他什么条件就再添加
		for (Contract ct : contractlist) {
			Long id = null;
			int delsign = 0;
			if (ct.getPid() == 0) {
				//说明是企业
				id = ct.getId();
			} else {
				//说明是人才
				id = ct.getPid();
				//查看这个人才对应的企业合同有没有收款记录
				int result = contractMoneylogDao.selectapproveskcount(id);
				ct.setCountsign(result);
			}
			int count = contractMoneylogDao.selectCountFromLog(id);
			if (count > 0) {
				delsign = 1;
			}
			ct.setDelsign(delsign);
		}
		return contractlist;
	}
	
	 

	@Override
	public Contract saveContract(Contract c) {
		//企业合同设置
		c.setPid(0L);
		c.setBill(0);
		c.setType(1);
		c.setState(Constants.ContactsState.ONE);
		c.setIscomplete(0); 
		Contract result = contractDao.insert(c);
		if(result.getId()!=null&&result.getId()>0){
			List<Contract> sublist = c.getSublist();
			for(Contract csub:sublist){
				//个人合同设置
				csub.setPid(result.getId());
				csub.setBill(0);
				//当挂靠合同时设置类型为个人
				if(csub.getContracttype()==1) {
					csub.setType(0);
					//根据人才证书金额判断付款状态
					if (csub.getTotalamount()==0) {
						//没有金额的 付款完成
						
						 int count = contractFilesService.getFileCountByConid(csub.getId());
							if(count<1) {
								csub.setState(Constants.ContactsState.SIX);
							}else {
								csub.setState(Constants.ContactsState.EIG);
							}
						
					} else {
						csub.setState(Constants.ContactsState.TOW);
					}
				}else {
					//资质及培训合同 子项合同设置为已完成
					csub.setState(Constants.ContactsState.EIG);
				}
			
				csub.setServicecharge(0);
				csub.setIscomplete(0);
				Contract subObj = contractDao.insert(csub);
				if(subObj!=null&&subObj.getId()!=null){
					/* 2017年12月1日更新，垫款的绑定移到回款那里，通过回款判断是否消除垫款记录 */   
						//ContractMoneylog  ml = new ContractMoneylog();
						//ml.setCreateuser(csub.getUid());
						//ml.setCusid(csub.getCusid());
					    //查看个人有没有垫款记录  有则更新垫款记录关联到合同
						//List<ContractMoneylog> mlResultlist = contractMoneylogService.getContractMoneylogInfoByContract(ml);
						//记录总垫款金额
						//int alltotalamount = 0;
						/*for (ContractMoneylog mlResult : mlResultlist) {
							ml = new ContractMoneylog();
							ml.setId(mlResult.getId());
							ml.setContractid(subObj.getId());
							contractMoneylogService.updateContractMoneylog(ml);
							alltotalamount += mlResult.getTotalamount();
						}*/
						//当垫款金额大于等于合同金额时   改变合同状态 为付款完成  否则为付款中
						/*if(alltotalamount>=csub.getTotalamount()){
							subObj.setState(Constants.ContactsState.SIX);
						} else if (subObj.getTotalamount() == 0) {
							//还有一个条件，如果人才证书价格为0改变合同状态为付款完成
							subObj.setState(Constants.ContactsState.SIX);
						} else{
							subObj.setState(Constants.ContactsState.FOUR);
						}*/
						//contractDao.updateByPrimaryKeySelective(subObj);
					
						
						
						 
						//保存完合同，更改客户到期时间
						CustomerInfo ci = new CustomerInfo();
						ci.setId(subObj.getCusid());
						ci.setEndtime(contractDao.selectByPrimaryKey(subObj).getEndtime());
						//更新用户信息
						customerDao.updateCustomerInfo(ci);
						
						//2017/6/23新增代办绑定,合同添加完成后,查询有没有设置代办,有的话绑定合同
						CommissionCustomer comcus = new CommissionCustomer();
						comcus.setCusid(ci.getId());
						
						List<CommissionCustomer> commlist = commissionCustomerDao.getCCInfoByParams(comcus);
						if (commlist != null && commlist.size() > 0) {
							for (CommissionCustomer cocu : commlist) {
								//绑定合同
								cocu.setConid(result.getId());
								cocu.setIsdel(0);
								commissionCustomerDao.updateCCinfo(cocu);
							}
						}
						
				//这里注释的是回扣，等以后流程通了再改		
				/*if(subObj!=null&&subObj.getId()!=null){
					//要判断垫款总额是不是大于人才证书价格 如果大于的话，那就是回扣？就不关联垫款
					if (csub.getAdvancemoney() > csub.getTotalamount()) {
						
					} else {
						ContractMoneylog  ml = new ContractMoneylog();
						//ml.setCreateuser(csub.getUid());
						ml.setCusid(csub.getCusid());
						//查看个人有没有垫款记录  有则更新垫款记录关联到合同
						List<ContractMoneylog> mlResultlist = contractMoneylogService.getContractMoneylogInfoByContract(ml);
						for (ContractMoneylog mlResult : mlResultlist) {
							ml = new ContractMoneylog();
							ml.setId(mlResult.getId());
							ml.setContractid(subObj.getId());
							contractMoneylogService.updateContractMoneylog(ml);
						}
					}
					subObj.setState(Constants.ContactsState.FOUR);
					contractDao.updateByPrimaryKeySelective(subObj);*/
			   } 
			}
		}
		return result;
	}



	@Override
	public Contract getContractInfo(Contract c) {
		return contractDao.selectByPrimaryKey(c);
	}



	@Override
	public int updateContract(Contract c) {
		return contractDao.updateByPrimaryKeySelective(c);
	}



	@Override
	public List<Contract> getAllContractListByPid(Contract c) {
		return contractDao.getAllContractListByPid(c);
	}



	@Override
	public int updateContractComplete(Contract c) {
		return contractDao.updateContractComplete(c);
	}



	@Override
	public boolean checkMoneyIsOver(Contract c) {
		c = contractDao.checkMoneyIsOver(c);
		if (c!=null && c.getTotalmoney() != null && c.getTotalmoney() >= c.getTotalamount()) {
			return true;
		} else {
			return false;
		}
	}



	@Override
	public int getApprovemoney(Long cusid,Long id) {
		Contract c = new Contract();
		c.setId(id);
		c.setCusid(id);
		Integer approvemoney = contractDao.getApprovemoney(c);
		return approvemoney;
	}



	@Override
	public List<Contract> getContractByPerformanceList(Contract c, int pageNo, int pageSize) {
		c.setStartsize((pageNo-1)*pageSize);
		c.setEndsize(pageSize);
		List<Contract> list = contractDao.getContractByPerformanceList(c);
		return list;
	}



	@Override
	public int getAllApprovemoney(Contract c) {
		return contractDao.getAllApprovemoney(c);
	}



	@Override
	public List<Contract> getContractByPerformanceList(Contract c) {
		return 	 contractDao.getContractByPerformanceList(c);
	}



	@Override
	public List<Contract> getServiceManageList(Contract c, int pageNo,
			int pageSize) {
		
		c.setStartsize((pageNo-1)*pageSize);
		c.setEndsize(pageSize);
		List<Contract> list = contractDao.getServiceManageList(c);
		return list;
	}



	@Override
	public int checkResurmeHaveContract(Long cusid) {
		return contractDao.checkResurmeHaveContract(cusid);
	}



	@Override
	public int getSigningByDay(Contract c) {
		return contractDao.getSigningByDay(c);
	}

	

}
